package com.isorf.controller;
import com.isorf.service.UploadService;
import com.github.pagehelper.PageInfo;
import com.isorf.pojo.adv;
import com.isorf.pojo.links;
import com.isorf.service.advservice;
import com.isorf.util.responseresult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
public class advcontroller {
    @Autowired
    private advservice advservice; // Variable name should match the field name
    @Autowired
    private UploadService uploadService;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;
    @PostMapping("/addadv")
    public ResponseEntity<?> addadv(@RequestBody adv adv) {
        if (adv.getCompanyname() == null || adv.getCompanyname().isEmpty()) {
            return ResponseEntity.badRequest().body("公司名称不能为空");
        }
        Boolean result = advservice.addadv(adv);
        if (result) {
            return ResponseEntity.ok("广告添加成功");
        } else {
            return ResponseEntity.badRequest().body("广告添加失败");
        }
    }


    @PostMapping("/uploadImageadv")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(
                    name = "flimage",
                    value = "上传的图片文件",
                    required = true,
                    paramType = "formData",
                    dataType = "MultipartFile",
                    dataTypeClass = MultipartFile.class,
                    defaultValue = "")})
    public responseresult uploadImage(@RequestPart MultipartFile flimage) {
        String flag = uploadService.uploadImage(path, flimage);
        if (flag!=null) {
            return responseresult.success(responseresult.SUCCESS, "上传成功", flag);
        }else{
            return responseresult.failure(responseresult.FAILED, "上传失败");
        }
    }

    @PostMapping("/pageeList")
    public responseresult<PageInfo<adv>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize)
    {
        PageInfo<adv> result = advservice.pageList(pageNum, pageSize);
        if (result == null || result.getList() == null || result.getList().size() == 0) {
            return responseresult.failure(responseresult.FAILED, "查询广告列表失败");
        } else {
            return responseresult.success(responseresult.SUCCESS,"查询成功", result);
        }
    }

    @PostMapping("/batchhDelete")
    public responseresult<Boolean> batchDelete(@RequestBody List<Integer> id) {
        // 检查参数
        if (id == null || id.isEmpty()) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的批量删除方法
        Boolean result = advservice.batchDelete(id);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "批量删除失败");
        }
    }

    @PostMapping("/deleteadv")
    public responseresult<Boolean> deleteadv(@RequestBody adv adv) {
        // 检查link对象和id是否为空
        if (adv == null || adv.getId() == null) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }


        Boolean result = advservice.deleteadv(adv.getId());
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "删除失败");
        }
    }

}